/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.tcc.view;

import br.com.tcc.dao.DAOLancamento;
import br.com.tcc.modelo.Lancamento;
import java.net.URL;
import java.text.NumberFormat;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.List;
import java.util.Objects;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.chart.PieChart;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TextField;
import javax.swing.JOptionPane;

/**
 * FXML Controller class
 *
 * @author Edidelson
 */
public class GraficoController implements Initializable {

    private final ObservableList<PieChart.Data> listaDeDespesas = FXCollections.observableArrayList();
    @FXML
    private PieChart chartDespesas;
    @FXML
    private DatePicker dtFinal;
    @FXML
    private DatePicker dtInicial;
    @FXML
    private TextField tfTotalGastos;

    /**
     * Initializes the controller class.
     *
     * @param url
     * @param rb
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        chartDespesas.setData(listaDeDespesas);
    }

    @FXML
    private void filtrar(ActionEvent event) {
        if (validarDatas()) {
            //Inicial
            listaDeDespesas.clear();
            LocalDate dataInicial = dtInicial.getValue();
            Calendar calendarInicial = Calendar.getInstance();
            calendarInicial.set(dataInicial.getYear(), dataInicial.getMonthValue() - 1, dataInicial.getDayOfMonth());
            //Final
            LocalDate dataFinal = dtFinal.getValue();
            Calendar calendarFinal = Calendar.getInstance();
            calendarFinal.set(dataFinal.getYear(), dataFinal.getMonthValue() - 1, dataFinal.getDayOfMonth());
            List<Lancamento> lancamentos = new DAOLancamento().filtroPorPeriodo(calendarInicial.getTime(), calendarFinal.getTime());
            if (!lancamentos.isEmpty()) {
                lancamentos.stream().forEach((lancamento) -> {
                    listaDeDespesas.add(new PieChart.Data(lancamento.toString(), lancamento.getValorLancamento()));
                });
            }

            double valorTotal = lancamentos.stream()
                    .filter((Lancamento l) -> Objects.equals(l.getValorLancamento(), l.getValorLancamento()))
                    .mapToDouble(lancamento -> lancamento.getValorLancamento())
                    .sum();
            tfTotalGastos.setText(NumberFormat.getCurrencyInstance().format(valorTotal));
        }
    }

    public boolean validarDatas() {
        if (dtInicial.getValue().isAfter(dtFinal.getValue())) {
            JOptionPane.showMessageDialog(null, "A data inicial não pode ser mair que a data final.");
            dtInicial.requestFocus();
            return false;
        }
        return true;
    }

}
